﻿using WRJClient.DAL;
using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WRJClient.Common;

namespace WRJClient.UI.SystemMonitor
{
    /// <summary>
    /// TopoTree.xaml 的交互逻辑
    /// </summary>
    public partial class TopoTree : UserControl
    {
        private bool _isWorkStation;
        public delegate void SelectedItemChangedHandler(object sender, RoutedPropertyChangedEventArgs<object> e);
        public event SelectedItemChangedHandler SelectedItemChanged;
        private ObservableCollection<TopoTreeNodeItem> _items = new ObservableCollection<TopoTreeNodeItem>();

        public TopoTree()
        {
            InitializeComponent();

            if (string.IsNullOrEmpty(Properties.Settings.Default.WorkStationId))
            {
                _isWorkStation = false;
            }
            else
            {
                _isWorkStation = true;
            }

            ControlCenterManager.Instance.OnDataAdded += Instance_OnDataAdded;
            WorkStationManager.Instance.OnDataAdded += Instance_OnDataAdded;
            UAVManager.Instance.OnDataAdded += Instance_OnDataAdded;
            MissionManager.Instance.OnDataAdded += Instance_OnDataAdded;

        }

        private void Instance_OnDataAdded(object sender, DataChangedEventArgs e)
        {
            BuildTree();
        }

        public void BuildTree()
        {
            _items.Clear();
            foreach (ControlCenterViewModel controlcenter in ControlCenterManager.Instance.Datas)
            {
                TopoTreeNodeItem controlcenterItem = new TopoTreeNodeItem(controlcenter);
                foreach (WorkStationViewModel workstation in WorkStationManager.Instance.Datas)
                {
                    TopoTreeNodeItem workstationItem = new TopoTreeNodeItem(workstation);
                    foreach (UAVViewModel uav in UAVManager.Instance.Datas)
                    {
                        if (uav.StaticData.WorkStationId == workstation.StaticData.DbId)
                        {
                            TopoTreeNodeItem uavItem = new TopoTreeNodeItem(uav);
                            foreach (MissionViewModel mission in MissionManager.Instance.Datas)
                            {
                                if (mission.StaticData.UavId == uav.StaticData.UavId && !mission.IsFinished)
                                {
                                    TopoTreeNodeItem missionItem = new TopoTreeNodeItem(mission);
                                    uavItem.Children.Add(missionItem);
                                }
                            }
                            workstationItem.Children.Add(uavItem);
                        }
                    }
                    if (_isWorkStation && Properties.Settings.Default.WorkStationId == workstation.StaticData.WorkStationId)
                    {
                        _items.Add(workstationItem);
                    }
                    else
                    {
                        controlcenterItem.Children.Add(workstationItem);
                    }
                }
                if (!_isWorkStation)
                {
                    _items.Add(controlcenterItem);
                }
            }

            topoTree.ItemsSource = _items;
        }

        public void BuildInfoTree()
        {
            _items.Clear();

            #region //指挥中心
            TopoTreeNodeItem controlcenters = new TopoTreeNodeItem("指挥中心");
            foreach (var vm in ControlCenterManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                controlcenters.Children.Add(item);
            }
            _items.Add(controlcenters);
            #endregion

            #region //工作站
            TopoTreeNodeItem workstations = new TopoTreeNodeItem("工作站");
            foreach (var vm in WorkStationManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                workstations.Children.Add(item);
            }
            _items.Add(workstations);
            #endregion

            #region //无人机
            TopoTreeNodeItem uavs = new TopoTreeNodeItem("无人机");
            foreach (var vm in UAVManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                uavs.Children.Add(item);
            }
            _items.Add(uavs);
            #endregion

            #region //任务
            TopoTreeNodeItem missions = new TopoTreeNodeItem("任务");
            foreach (var vm in MissionManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                missions.Children.Add(item);
            }
            _items.Add(missions);
            #endregion

            #region //空域
            TopoTreeNodeItem zones = new TopoTreeNodeItem("空域");
            foreach (var vm in ZoneManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                zones.Children.Add(item);
            }
            _items.Add(zones);
            #endregion

            #region //航线
            TopoTreeNodeItem airlines = new TopoTreeNodeItem("航线");
            foreach (var vm in AirlineManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                airlines.Children.Add(item);
            }
            _items.Add(airlines);
            #endregion

            #region //情报
            TopoTreeNodeItem infomations = new TopoTreeNodeItem("情报");
            foreach (var vm in InfomationManager.Instance.Datas)
            {
                TopoTreeNodeItem item = new TopoTreeNodeItem(vm);
                infomations.Children.Add(item);
            }
            _items.Add(infomations);
            #endregion

            topoTree.ItemsSource = _items;
        }

        private void topoTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            SelectedItemChanged?.Invoke(sender, e);
        }
    }
}
